package com.bbx.taxi.client.Activity.Base;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ZoomControls;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.model.LatLng;
import com.bbx.androidapi.util.SharedPreUtil;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.R;

/** 
 * @Path com.bbx.taxi.client.Activity.Base.BaiduMap.java
 * @Author 严旭文
 * @Date 2016-7-13 下午2:44:01
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 */
public class MyBaiduMap {
	public static void initDefaultLocation(final Context context,final MapView mMapView) {
		boolean isFirstLoc = true;// 是否首次定位
		final BaiduMap mBaiduMap;
		try{
		mMapView.showZoomControls(false);
		mMapView.showScaleControl(false);
		if (mMapView != null&& mMapView.getMap()!=null) {
			mBaiduMap = mMapView.getMap();

			mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
					LocationMode.NORMAL, true, null));
			String str_latitude = SharedPreUtil.getStringValue(context,SharedPreEncryptUtil.latitude,"");
			String str_longitude = SharedPreUtil.getStringValue(context,SharedPreEncryptUtil.longitude,"");
			if (MyApplication.getInstance().mLocation != null||(!str_latitude.equals("")&&!str_longitude.equals(""))) {// 先默认定位
				float radius=1000;
				double latitude=0;
				double longitude=0;
				double direction=0;
				if(MyApplication.getInstance().mLocation != null){
					radius=MyApplication.getInstance().mLocation.getRadius();
					latitude=MyApplication.getInstance().mLocation
							.getLatitude();
					longitude=MyApplication.getInstance().mLocation
							.getLongitude();
					direction=MyApplication.getInstance().mLocation.getDirection();
				}else{
					try{
					latitude=Double.parseDouble(str_latitude);
					longitude=Double.parseDouble(str_longitude);
					}catch(Exception e){}
				}
				MyLocationData locData = new MyLocationData.Builder()
						.accuracy(0)
						// 此处设置开发者获取到的方向信息，顺时针0-360
						.direction((int)direction)
						.latitude(latitude)
						.longitude(longitude).build();
				mBaiduMap.setMyLocationData(locData);
				if (isFirstLoc) {
					int zoom=18;
					isFirstLoc = false;
					LatLng ll= new LatLng(latitude,longitude);
					//定位失败
					if(ll.latitude==0||ll.longitude==0){
						MyApplication mApplication = MyApplication.getInstance();
						ll= mApplication.ll_fail;
						zoom=4;
					}
					MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
					MapStatusUpdate u2 = MapStatusUpdateFactory.zoomTo(zoom);
					mBaiduMap.animateMapStatus(u);
					mBaiduMap.animateMapStatus(u2);
				}
				if(MyApplication.getInstance().mLocation!=null){
					//存储数据
					SharedPreEncryptUtil.putStringValue(context, SharedPreEncryptUtil.latitude, MyApplication.getInstance().mLocation.getLatitude()+"");
					SharedPreEncryptUtil.putStringValue(context, SharedPreEncryptUtil.longitude, MyApplication.getInstance().mLocation.getLongitude()+"");
				}
			}else{
				new Handler().postDelayed(new Runnable() {
					
					@Override
					public void run() {
						initDefaultLocation(context, mMapView);
					}
				}, 2000);
				
			}
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					if(mBaiduMap!=null){
						mBaiduMap.setMyLocationEnabled(true);
						mBaiduMap
						.setMyLocationConfigeration(new MyLocationConfiguration(
								LocationMode.NORMAL, true,
								BitmapDescriptorFactory
										.fromResource(R.drawable.icon_mapid2)));
					}
				
				}
			}, 100);
		}else{
			new Handler().postDelayed(new Runnable() {
				
				@Override
				public void run() {
					initDefaultLocation(context, mMapView);
				}
			}, 2000);
		}
		// 隐藏logo
		View child = mMapView.getChildAt(1);
		if (child != null && (child instanceof ImageView || child instanceof ZoomControls)){            
		     child.setVisibility(View.INVISIBLE);           
		}
		}
		catch(Exception e){}
	

	}

}
